<?php
namespace Ygcl\Controller;
use Admin\Controller\AdminbaseController;
class TeacheradminController extends AdminbaseController {
	
	protected $m;
	protected $usersLogic;
	
	function _initialize() {
		parent::_initialize();
		$this->m = D('Users');
		$this->usersLogic = D('Users','Logic');
	}
	
    public function index(){
    	$uids = D('UsersDistrictRelation')->where(array('did'=>I('did')))->getField('uid',true);
		$this->uids = implode(',',$uids);
    	$this->display();
    }
    
    public function edit(){
    	$this->class = D('YgclClass')->where(array('did'=>I('did')))->field('id,title')->select();
    	$did = I('did');
    	$id = I('get.uid');
    	if($id){
    		$map['uid'] = $id;
    		$this->info = $this->m->where($map)->find();
    		$this->udr = D('UsersDistrictRelation')->where($map)->find();
    		$this->class = D('YgclClass')->where(array('did'=>$this->udr['did']))->field('id,title')->select();
    		$did = $this->udr['did'];
    	}
    	$this->did = $did;
    	$this->display();
    }
    
    public function edit_post(){
    	if(IS_POST){
    		$_POST['update_time'] = Date('Y-m-d H:i:s');
    		if($_POST['uid']){
    			if($_POST['password']){
    				$_POST['password'] = md5($_POST['password']);
    			}else{
    				unset($_POST['password']);
    			}
    			if($this->m->create()){
    				if($this->m->save()){
    					$this->usersLogic->_save_relation($_POST['uid'],$_POST['did'],$_POST['cid']);
    					$this->success('更新成功');
    				}else{
    					$this->error('更新失败');
    				}
    			}
    		}else{
    			if($_POST['password']){
    				$_POST['password'] = md5($_POST['password']);
    			}else{
    				$_POST['password'] = md5('666666');
    			}
    			$_POST['type'] = 'add';
    			$_POST['role'] = 'teacher';
    			
    			$info = $this->m->where(array('mobile'=>$_POST['mobile']))->find();
    			if($info){
    				$data = array(
    					'uid' => $info['uid'],
    					'realname' => $_POST['realname'],
    					'mobile' => $_POST['mobile'],
    					'username' => $_POST['username'],
    					'password' => $_POST['password'],
    					'update_time' => $_POST['update_time'],
    					'role' => $_POST['role'],
    				);
    				if($this->m->save($data)){
    					$this->usersLogic->_save_relation($info['uid'],$_POST['did'],$_POST['cid']);
    					$this->success('更新成功');
    				}else{
    					$this->error('更新失败');
    				}
    			}else{
    				$_POST['create_time'] = $_POST['update_time'];
    				if($this->m->create()){
    					$id = $this->m->add();
    					if($id){
    						$this->usersLogic->_save_relation($id,$_POST['did'],$_POST['cid']);
    						$this->success('添加成功');
    					}else{
    						$this->error('添加失败');
    					}
    				}
    			}
    		}
    	}
    }
    
    public function del(){
    	$id = array_unique((array)I('ids',0));
    	if ( empty($id) ) {
    		$this->error('请选择要操作的数据!');
    	}
    	$map = array('uid' => array('in', $id));
    	if($this->m->where($map)->delete()){
    		$this->success('删除成功',U('index'));
    	} else {
    		$this->error('删除失败！');
    	}
    }
    
    public function changestatus(){
    	$data[$this->m->getPk()] = $_REQUEST['uid'];
    	$data['status'] = $_REQUEST['status'];
    	$data['update_time'] = date('Y-m-d H:i:s');
    	if($this->m->save($data)){
    		$this->success('状态更新成功');
    	}else{
    		$this->error('状态更新失败');
    	}
    }
}